package com.lwh.springboot.controller;
import com.lwh.springboot.service.IRentService;
import com.lwh.springboot.pojo.entity.Rent;
import com.lwh.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/rent")
public class RentController {
    @Autowired
    private IRentService rentService;

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model){
        Rent rent=rentService.selectById(id);
        model.addAttribute("rent",rent);
        return "/rent/update";
    }
    @RequestMapping("/update")
    public String update(Rent rent){
        rentService.update(rent);
        return "redirect:/rent/selectByPage";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "/rent/add";
    }
    @RequestMapping("/add")
    public String add(Rent rent){
        rentService.add(rent);
        return "redirect:/rent/selectByPage";
    }
    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        rentService.deleteAll(ids);
        return "redirect:/rent/selectByPage";
    }
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        rentService.deleteById(id);
        return "redirect:/rent/selectByPage";
    }
    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "5") Integer pageSize, Model model) {
        System.out.println("RentController.selectByPage");
        PageInfo pageInfo = rentService.selectByPage(pageNo, pageSize);

        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        return "/rent/list";
    }
}
